package turretbehavior 
{
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author morgan
	 */
	public class SpinningTurretBehavior extends TurretBehavior
	{
		public var anglePerSec:Number;
		public var rotateUntil:Number;
		public var changeDirections:Boolean = false;
		public var backwards:Boolean = false;
		public var rotated:Number = 0;
		public function SpinningTurretBehavior(anglePerSec:Number, rotateUntil:Number = 360, changeDirections:Boolean = false) 
		{
			this.anglePerSec = anglePerSec;
			this.rotateUntil = rotateUntil;
			this.changeDirections = changeDirections;
			this.rotated = 0;
		}
		
		override public function onUpdate(t:Turret):void 
		{
			super.onUpdate(t);
			t.fireAngle += (backwards ? -1 : 1) * anglePerSec * FlxG.elapsed;
			rotated += (backwards ? -1 : 1) * Math.abs(anglePerSec) * FlxG.elapsed;
			if (rotated >= rotateUntil)
			{
				if (changeDirections)
				{
					backwards = true;
				}
				else
				{
					rotated -= rotateUntil;
					t.fireAngle -= rotateUntil;
				}
			}
			if (backwards && rotated < 0)
			{
				backwards = false;
			}
		}	
	}
}
